home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: <75151.03563@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: re: tricky questions
- Date: 3 Jan 1996 02:32:43 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4ccpsc$dnf@dub-news-svc-4.compuserve.com>
- NNTP-Posting-Host: dd05-017.compuserve.com
- Content-Type: text/plain
- Content-length: 1588
- X-Newsreader: AIR Mosaic (16-bit) version 3.10.08.25
-
-
- RE Q1:
-
- the default ctor of class A leaves the pointer variable , A* a,
- uninitialized. This is almost always a bad practice. You should
- program so that pointers always have a <valid> value, ie they point
- somewhere sensible, or they are NULL. In your example, when you
- create an object of class B, A's default contructor is called, which leaves
- the member pointer variable A *a in an unknown state. When the
- object is destroyed, the destructor tries to delete am invalid pointer - a sure
- recipe for disaster. The reason you did not get the crash when you
- set a= 0 in the deafult ctor is that you were doing the right thing, delete
- knows how to handle a NULL pointer.
- A habit I've gotten into which has saved a lot of grief is to always
- initialize pointer variable to NULL in the member initialization list of the
- ctor, ie. I would have written the ctors like this.
-
- A::A() :
- a( NULL )
- {
- }
-
- A::A(int i) :
- a( NULL )
- {
- a= new B( i );
- }
-
- doing this in the second case is a bit redundant, but the habit has saved
- me enough grief to make it worth the effort.
-
- Q2:
- I'm not as sure of this one, but... I believe that when you have an
- expression that is made up of several smaller expressions that are
- separated by commas, the overall expression takes on the value of
- the rightmost sub-expression. So in your example
- p= (4, 5)
- you have two expressions ( '4' and '5' ) separated by commas, so the
- value of the total expression is that of the rightmost expression, which is
- 5.
-
- Hope this helps,
- Tom Keane
- 75151,03563@compuserve.com
-
-
-